package com.jeanbart.GUI;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.*;

import com.jeanbart.bean.Player;
import com.jeanbart.service.ClientService;
import com.jeanbart.util.AgetB;
import com.jeanbart.util.ReadReplyFile;
import com.jeanbart.util.WowsSes;
import com.melloware.jintellitype.HotkeyListener;
import com.melloware.jintellitype.JIntellitype;

/**
 * 利用JIntellitype实现全局热键设置
 * @author Jeby Sun
 *这里是按ALT+O的
 */
public class MainIframe extends JFrame {

	private static final long serialVersionUID = 1L;
	
	//定义热键标识，用于在设置多个热键时，在事件处理中区分用户按下的热键
	public static final int FUNC_KEY_MARK = 1;
	
	JLabel msgBtn;
	JLabel exitBtn;

	public MainIframe() {
		AgetB agetB = new AgetB();
		this.setTitle("wows胜率显示");
		this.setSize(320,170);
		this.setLocationRelativeTo(null);
		this.setLayout(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		new WowsSes().ScanReplayFile(false);

		msgBtn = new JLabel("胜率显示（Alt+6）");
		msgBtn.setFocusable(false);
		msgBtn.setBounds(20, 20, 270, 50);
		this.add(msgBtn);
		
		exitBtn = new JLabel("显示关闭（Alt+5）");
		exitBtn.setFocusable(false);
		exitBtn.setBounds(20, 60, 270, 50);
		this.add(exitBtn);

		systemTray(); // 设置系统托盘
		// 添加关闭按钮事件，关闭时候实质是把窗体隐藏
		this.addWindowListener(new WindowAdapter() {
				@Override
				public void windowIconified(WindowEvent e) {
					MainIframe.this.setVisible(false);
				}
			});
        //第一步：注册热键，第一个参数表示该热键的标识，第二个参数表示组合键，如果没有则为0，第三个参数为定义的主要热键
    	JIntellitype.getInstance().registerHotKey(FUNC_KEY_MARK, JIntellitype.MOD_ALT, (int)'6');
    	//第二步：添加热键监听器
		JIntellitype.getInstance().addHotKeyListener(new HotkeyListener() {
			@Override
			public void onHotKey(int markCode) {
				switch (markCode) {  
		        case FUNC_KEY_MARK:
					List<List<Player>> list = null;
					try {
						list = new ReadReplyFile().startRead(false);
					} catch (IOException e) {
						e.printStackTrace();
					}
					List<Player> Teammate=list.get(0);
					List<Player> Enemy=list.get(1);
					FrameLR.getInstance(Teammate,Enemy);
		            break;
				} 				
			}
		});
		this.setVisible(true);
	}
	private void systemTray() {
		SystemTray tray = SystemTray.getSystemTray();
		if (SystemTray.isSupported()) { // 判断系统是否支持托盘功能.
			// 创建托盘右击弹出菜单
			PopupMenu popupMenu = new PopupMenu();
			//创建弹出菜单中的退出项
			MenuItem itemExit = new MenuItem("exit");
			itemExit.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					System.exit(0);
				}
			});
			popupMenu.add(itemExit);
			//创建托盘图标
			File file = new File(System.getProperty("user.dir"));
			String iconPath=file.getParent()+"/icon.jpg";
			ImageIcon icon = new ImageIcon(iconPath); // 创建图片对象
			TrayIcon trayIcon = new TrayIcon(icon.getImage(), "胜率显示",
					popupMenu);
			trayIcon.setImageAutoSize(true);
			trayIcon.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					MainIframe.this.setVisible(true);
				}
			});
			try {
				tray.add(trayIcon);
			} catch (AWTException e) {
				e.printStackTrace();
			}
		}
	}
}
